{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "_llm = ChatOpenAI(\n",
    "    api_key=\"ollama\",\n",
    "    model=\"qwen2.5:7b\",\n",
    "    base_url=\"http://192.168.10.13:60001/v1\",\n",
    "    temperature=0.7,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_llm.invoke(\"今天上海的天气如何\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Annotated\n",
    "from langchain_core.tools import tool\n",
    "\n",
    "\n",
    "@tool\n",
    "def weather(city: Annotated[str, \"被查询的城市,用中文输入\"]) -> str:\n",
    "    \"\"\"\n",
    "    用于查询输入城市今日的天气状况\n",
    "    \"\"\"\n",
    "    if city == \"上海\":\n",
    "        return \"上海今天台风12级\"\n",
    "    else:\n",
    "        return \"天气晴朗风和日丽\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "_tools = [weather,]\n",
    "_llm_with_tools = _llm.bind_tools(_tools)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "_messages = ChatPromptTemplate(\n",
    "    [(\"system\", \"你是一个翻译官,把用户的输入翻译成{template}\"), (\"human\", \"{content}\")]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.messages import ToolMessage\n",
    "\n",
    "_messages = [(\"human\", \"今天上海的天气如何?\")]\n",
    "rep = _llm_with_tools.invoke(_messages)\n",
    "rep.tool_calls\n",
    "\n",
    "if rep.tool_calls is not None or len(rep.too_calls) > 0:\n",
    "    for _tool in rep.tool_calls:\n",
    "        _fun = eval(_tool[\"name\"])\n",
    "        _tool_rep = _fun.invoke(_tool[\"args\"])\n",
    "        _messages.append(_tool_rep)\n",
    "        # _messages.append(ToolMessage(_tool_rep))\n",
    "\n",
    "_llm.invoke(_messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "_srtOutputParser = StrOutputParser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rep = _messages.invoke(\n",
    "    {\"language\": \"德文\", \"content\": \"我爱北京天安门，天安门上太阳升\"}\n",
    ")\n",
    "_llm.invoke(rep)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_chain =_messages|_llm|_srtOutputParser\n",
    "_chain.invoke()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
